package com.zhr.server;

import java.io.IOException;
import java.util.Scanner;

import com.zhr.httpd.WeixinHTTPD;
import com.zhr.listener.MessageEventListener;
import com.zhr.listener.MessageImageListener;
import com.zhr.listener.MessageLinkListener;
import com.zhr.listener.MessageListenerContainer;
import com.zhr.listener.MessageLocationListener;
import com.zhr.listener.MessageTextListener;

public class WeixinServer {

	private MessageListenerContainer listenerContainer;

	public WeixinServer(int port) throws IOException {
		// server = new ServerSocket(port);
		registListener();
		new WeixinHTTPD(port, this);
		try {
			System.in.read();
		} catch (Exception e) {
		}
	}

	private void registListener() {
		listenerContainer = new MessageListenerContainer();
		listenerContainer.registListeners(new MessageEventListener(),
				new MessageImageListener(), new MessageLocationListener(),
				new MessageLinkListener(), new MessageTextListener());
	}

	public WeixinResponse execute(WeixinRequest request) {
		return listenerContainer.execute(request);
	}

	public static void main(String args[]) {
		try {
			int port = 81;
			try {
				if (null != args) {
					port = Integer.parseInt(args[0]);
				}
			} catch (Exception e) {
			}
			new WeixinServer(port);
			Scanner scanner = new Scanner(System.in);
			while (scanner.hasNext() && !"exit".equals(scanner.nextLine()))
				;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
